/*
 *	Filename: Quadrotor_eCap.c
 *	Fuction: Configure eCap Module to Signals from Remote Controllor
 *	Modified: 2016-11-09
 *	Author: Bin
 */

#include "DSP28x_Project.h"
#include "Quadrotor_eCap.h"

// Internal Variable Definition
static Uint16 cap1Value = 0, cap2Value = 0, cap3Value = 0, cap4Value = 0;

// Get capValue Fuction
Uint16 getCap1() {return cap1Value;}
Uint16 getCap2() {return cap2Value;}
Uint16 getCap3() {return cap3Value;}
Uint16 getCap4() {return cap4Value;}

extern void InitECapGpioAll(void) {
	// eCap1, eCap2 ,eCap3, eCap4 are Used	
	InitECap1Gpio();
	InitECap2Gpio();
	InitECap3Gpio();
	InitECap4Gpio();
}

extern void InitECapAll(void) {
	// eCap1, eCap2 ,eCap3, eCap4 are Used
	InitECapx(&ECap1Regs);
	InitECapx(&ECap2Regs);
	InitECapx(&ECap3Regs);
	InitECapx(&ECap4Regs);	
}

void InitECap1Gpio(void) {

	EALLOW;

	GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;		// Enable pull-up on GPIO5 (CAP1)
//	GpioCtrlRegs.GPAPUD.bit.GPIO24 = 0;		// Enable pull-up on GPIO24 (CAP1)
//	GpioCtrlRegs.GPBPUD.bit.GPIO34 = 0;		// Enable pull-up on GPIO34 (CAP1)

	GpioCtrlRegs.GPAQSEL1.bit.GPIO5 = 0;	// Synch to SYSCLKOUT GPIO5 (CAP1)
//	GpioCtrlRegs.GPAQSEL2.bit.GPIO24 = 0;	// Synch to SYSCLKOUT GPIO24 (CAP1)
//	GpioCtrlRegs.GPBQSEL1.bit.GPIO34 = 0;	// Synch to SYSCLKOUT GPIO34 (CAP1)

	GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 3;		// Configure GPIO5 as CAP1
//	GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 1;	// Configure GPIO24 as CAP1
//	GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 1;	// Configure GPIO34 as CAP1

	EDIS;
}

void InitECap2Gpio(void) {

	EALLOW;

	GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;		// Enable pull-up on GPIO7 (CAP2)
//	GpioCtrlRegs.GPAPUD.bit.GPIO25 = 0;		// Enable pull-up on GPIO25 (CAP2)
//	GpioCtrlRegs.GPBPUD.bit.GPIO37 = 0;		// Enable pull-up on GPIO37 (CAP2)

	GpioCtrlRegs.GPAQSEL1.bit.GPIO7 = 0;	// Synch to SYSCLKOUT GPIO7 (CAP2)
//	GpioCtrlRegs.GPAQSEL2.bit.GPIO25 = 0;	// Synch to SYSCLKOUT GPIO25 (CAP2)
//	GpioCtrlRegs.GPBQSEL1.bit.GPIO37 = 0;	// Synch to SYSCLKOUT GPIO37 (CAP2)

	GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 3;		// Configure GPIO7 as CAP2
//	GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 1;	// Configure GPIO25 as CAP2
//	GpioCtrlRegs.GPBMUX1.bit.GPIO37 = 1;	// Configure GPIO37 as CAP2

	EDIS;
}

void InitECap3Gpio(void) {

	EALLOW;

	GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0;		// Enable pull-up on GPIO9 (CAP3)
//	GpioCtrlRegs.GPAPUD.bit.GPIO26 = 0;		// Enable pull-up on GPIO26 (CAP3)

	GpioCtrlRegs.GPAQSEL1.bit.GPIO9 = 0;	// Synch to SYSCLKOUT GPIO9 (CAP3)
//	GpioCtrlRegs.GPAQSEL2.bit.GPIO26 = 0;	// Synch to SYSCLKOUT GPIO26 (CAP3)

	GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 3;		// Configure GPIO9 as CAP3
//	GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 1;	// Configure GPIO26 as CAP3

	EDIS;
}

void InitECap4Gpio(void) {

	EALLOW;

	GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0;		// Enable pull-up on GPIO11 (CAP4)
//	GpioCtrlRegs.GPAPUD.bit.GPIO27 = 0;		// Enable pull-up on GPIO27 (CAP4)

	GpioCtrlRegs.GPAQSEL1.bit.GPIO11 = 0;	// Synch to SYSCLKOUT GPIO11 (CAP4)
//	GpioCtrlRegs.GPAQSEL2.bit.GPIO27 = 0;	// Synch to SYSCLKOUT GPIO27 (CAP4)

	GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 3;	// Configure GPIO11 as CAP4
//	GpioCtrlRegs.GPAMUX2.bit.GPIO27 = 1;	// Configure GPIO27 as CAP4

	EDIS;
}

void InitECapx(volatile struct ECAP_REGS *regAddr) {
	// Disable all
	regAddr->ECEINT.all = 0x0000;			// Disable all capture interrupts
	regAddr->ECCLR.all = 0xFFFF;			// Clear all CAP interrupt flags
	regAddr->ECCTL1.bit.CAPLDEN = 0;		// Disable CAP1-CAP4 register loads
	regAddr->ECCTL2.bit.TSCTRSTOP = 0;		// Make sure the counter is stopped

	// Configure Peripheral Registers
	regAddr->ECCTL1.bit.CAP1POL = EC_RISING;
	regAddr->ECCTL1.bit.CAP2POL = EC_FALLING;
	regAddr->ECCTL1.bit.CAP3POL = EC_RISING;
	regAddr->ECCTL1.bit.CAP4POL = EC_FALLING;
	regAddr->ECCTL1.bit.CTRRST1 = EC_DELTA_MODE;
	regAddr->ECCTL1.bit.CTRRST2 = EC_DELTA_MODE;
	regAddr->ECCTL1.bit.CTRRST3 = EC_DELTA_MODE;
	regAddr->ECCTL1.bit.CTRRST4 = EC_DELTA_MODE;
	regAddr->ECCTL1.bit.CAPLDEN = EC_ENABLE;
	regAddr->ECCTL1.bit.PRESCALE = EC_DIV1;

	regAddr->ECCTL2.bit.CAP_APWM = EC_CAP_MODE;
	regAddr->ECCTL2.bit.CONT_ONESHT = EC_CONTINUOUS;
	regAddr->ECCTL2.bit.SYNCO_SEL = EC_SYNCO_DIS;
	regAddr->ECCTL2.bit.SYNCI_EN = EC_DISABLE;
	regAddr->ECCTL2.bit.TSCTRSTOP = EC_RUN;

	regAddr->ECEINT.bit.CEVT2 = 1;
	regAddr->ECEINT.bit.CEVT4 = 1;
}

interrupt void ecap1_isr(void) {

	if(ECap1Regs.ECFLG.bit.CEVT2) {
		cap1Value = ECap1Regs.CAP2 / 150;
		ECap1Regs.ECCLR.bit.CEVT2 = 1;
	}
	if(ECap1Regs.ECFLG.bit.CEVT4) {
		cap1Value = ECap1Regs.CAP4 / 150;
		ECap1Regs.ECCLR.bit.CEVT4 = 1;
	}

	//ECap1Regs.ECCLR.bit.CEVT2 = 1;
	//ECap1Regs.ECCLR.bit.CEVT4 = 1;
	ECap1Regs.ECCLR.bit.INT = 1;

	// Acknowledge this interrupt to receive more interrupts from group 4
	PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
}

interrupt void ecap2_isr(void) {

   if(ECap2Regs.ECFLG.bit.CEVT2) {
      cap2Value = ECap2Regs.CAP2 / 150;
      ECap2Regs.ECCLR.bit.CEVT2 = 1;
   }
   if(ECap2Regs.ECFLG.bit.CEVT4) {
      cap2Value = ECap2Regs.CAP4 / 150;
      ECap2Regs.ECCLR.bit.CEVT4 = 1;
   }

   //ECap2Regs.ECCLR.bit.CEVT2 = 1;
   //ECap2Regs.ECCLR.bit.CEVT4 = 1;
   ECap2Regs.ECCLR.bit.INT = 1;

   // Acknowledge this interrupt to receive more interrupts from group 4
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
}

interrupt void ecap3_isr(void) {

   if(ECap3Regs.ECFLG.bit.CEVT2) {
      cap3Value = ECap3Regs.CAP2 / 150;
      ECap3Regs.ECCLR.bit.CEVT2 = 1;
   }
   if(ECap3Regs.ECFLG.bit.CEVT4) {
      cap3Value = ECap3Regs.CAP4 / 150;
      ECap3Regs.ECCLR.bit.CEVT4 = 1;
   }

   //ECap3Regs.ECCLR.bit.CEVT2 = 1;
   //ECap3Regs.ECCLR.bit.CEVT4 = 1;
   ECap3Regs.ECCLR.bit.INT = 1;

   // Acknowledge this interrupt to receive more interrupts from group 4
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
}

interrupt void ecap4_isr(void) {

   if(ECap4Regs.ECFLG.bit.CEVT2) {
      cap4Value = ECap4Regs.CAP2 / 150;
      ECap4Regs.ECCLR.bit.CEVT2 = 1;
   }
   if(ECap4Regs.ECFLG.bit.CEVT4) {
      cap4Value = ECap4Regs.CAP4 / 150;
      ECap4Regs.ECCLR.bit.CEVT4 = 1;
   }

   //ECap4Regs.ECCLR.bit.CEVT2 = 1;
   //ECap4Regs.ECCLR.bit.CEVT4 = 1;
   ECap4Regs.ECCLR.bit.INT = 1;

   // Acknowledge this interrupt to receive more interrupts from group 4
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
}
